using System;
using UIFramework;
using UnityEngine;
using Utils;

namespace UIFramework.Examples {
    
    /// <summary>
    /// 模板入口
    /// </summary>
    public class UIDemoController : MonoBehaviour
    {
        [SerializeField] private UISettings defaultUISettings = null;
        [SerializeField] private FakePlayerData fakePlayerData = null;
        [SerializeField] private Camera cam = null;
        [SerializeField] private Transform transformToFollow = null;
    
        private UIFrame uiFrame;

        private void Awake()
        {
            uiFrame = defaultUISettings.CreateUIInstance();
        
            Signals.Get<StartDemoSignal>().AddListener(OnStartDemo);
            Signals.Get<NavigateToWindowSignal>().AddListener(OnNavigateToWindow);
            Signals.Get<ShowConfirmationPopupSignal>().AddListener(OnShowConfirmationPopup);
            
            Signals.Get<ShowTestWindowOtherSignal>().AddListener(OnTestWindowOther);
            Signals.Get<ShowTestWindowOther2Signal>().AddListener(OnTestWindowOther2);

            Signals.Get<ShowTaskWindowSignal>().AddListener(OnTaskWindow);
            Signals.Get<ShowRewardWindowSignal>().AddListener(OnRewardWindow);
        }
    
        private void OnDestroy() 
        {
            Signals.Get<StartDemoSignal>().RemoveListener(OnStartDemo);
            Signals.Get<NavigateToWindowSignal>().RemoveListener(OnNavigateToWindow);
            Signals.Get<ShowConfirmationPopupSignal>().RemoveListener(OnShowConfirmationPopup);
            
            Signals.Get<ShowTestWindowOtherSignal>().RemoveListener(OnTestWindowOther);
            Signals.Get<ShowTestWindowOther2Signal>().RemoveListener(OnTestWindowOther2);
            
            Signals.Get<ShowRewardWindowSignal>().RemoveListener(OnTaskWindow);
            Signals.Get<ShowRewardWindowSignal>().RemoveListener(OnRewardWindow);
        }
    
        private void Start() 
        {
            uiFrame.OpenWindow(ScreenIds.StartGameWindow);
        }
    
        private void OnStartDemo() 
        {
            uiFrame.ShowPanel(ScreenIds.NavigationPanel);
            uiFrame.ShowPanel(ScreenIds.ToastPanel);
        }
        
        /// <summary>
        /// 打开导航栏目
        /// </summary>
        /// <param name="windowId"></param>
        private void OnNavigateToWindow(string windowId) 
        {
            // 关闭当前界面
            uiFrame.CloseCurrentWindow();

            switch (windowId) {
                case ScreenIds.PlayerWindow:
                    uiFrame.OpenWindow(windowId, new PlayerWindowProperties(fakePlayerData.LevelProgress));
                    break;
                case ScreenIds.CameraProjectionWindow:
                    transformToFollow.parent.gameObject.SetActive(true);
                    uiFrame.OpenWindow(windowId, new CameraProjectionWindowProperties(cam, transformToFollow));
                    break;
                default:
                    uiFrame.OpenWindow(windowId);
                    break;
            }
        }

        /// <summary>
        /// 打开Confirmation弹窗
        /// </summary>
        /// <param name="popupPayload"></param>
        private void OnShowConfirmationPopup(ConfirmationPopupProperties popupPayload) 
        {
            uiFrame. OpenWindow(ScreenIds.ConfirmationPopup, popupPayload);
        }

        /// <summary>
        /// 打开队列测试窗口1
        /// </summary>
        private void OnTestWindowOther()
        {
            uiFrame.OpenWindow(ScreenIds.TestWindowOther);
        }
        
        /// <summary>
        /// 打开队列测试窗口2
        /// </summary>
        private void OnTestWindowOther2()
        {
            uiFrame.OpenWindow(ScreenIds.TestWindowOther2);
        }
        
        /// <summary>
        /// 打开任务窗口
        /// </summary>
        private void OnTaskWindow()
        {
            uiFrame.OpenWindow(ScreenIds.TaskWindow);
        }

        /// <summary>
        /// 领取奖励事件
        /// </summary>
        private void OnRewardWindow()
        {
            uiFrame.OpenWindow(ScreenIds.RewardExpWindow);
            uiFrame.OpenWindow(ScreenIds.RewardCoinWindow);
            uiFrame.OpenWindow(ScreenIds.RewardHeartWindow);
        }
        
    }
    
    
}

